<template>
    <el-dialog :title="title" :visible.sync="isDeitOpen" width="50%" style="margin-top:4%" append-to-body
        :before-close="closeDelDialog">
        <el-form :rules="rules" ref="ruleForm" :model="form" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="档案名称" prop="title">
                        <el-input v-model="form.title" placeholder="请输入档案名称" maxlength="30" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="移交人" prop="userName">
                        <el-input v-model="form.userName" placeholder="请输入移交人" maxlength="30" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="档案门类" prop="categoryId">
                        <treeselect v-model="form.categoryId" :options="deptOptions" :disable-branch-nodes="true" label
                            placeholder="请选择档案门类" noOptionsText="暂无数据" :normalizer="normalizerCategoryID"
                            :style="isStyle ? 'line-height: 32px;font-size:12px' : 'line-height: 36px;font-size:14px'" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="介质" prop="medium">
                        <el-select v-model="form.medium" placeholder="请选择" @keyup.enter.native="handleQuery"
                            style="width:100% ;">
                            <el-option v-for="(i, idx) in dict.type.JZ" :key="idx" :label="i.label" :value="i.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="条目数目" prop="entriesNum">
                        <el-input v-model.number="form.entriesNum" placeholder="请输入条目数" type="number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="操作数目" prop="operationsNum">
                        <el-input v-model.number="form.operationsNum" placeholder="请输入操作数量" type="number"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="接收时间" prop="receptionTime">
                        <el-date-picker v-model="form.receptionTime" type="date" placeholder="选择日期" style="width:100% ;"
                            format="yyyyMMdd" value-format="yyyyMMdd">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="移交单位" prop="transferUnit">
                        <treeselect v-model="form.transferUnit" :options="optionSelect" :disable-branch-nodes="true" label
                            placeholder="请选择移交单位" noOptionsText="暂无数据" :normalizer="normalizerCategoryID"
                            :style="isStyle ? 'line-height: 32px;font-size:12px' : 'line-height: 36px;font-size:14px'" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="notes">
                        <el-input v-model="form.notes" type="textarea" style="width: 100%" :rows="6"
                            placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">保 存</el-button>
            <el-button @click="closeDelDialog">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { listGROUPTree } from "@/api/archive/archivalAppraisal/openness/openArchivesAppraisal"//档案门类树
import { archiveEnroll, archiveEnrollDetile,archiveEnrollDeit } from "@/api/archive/digitalizeReceive/archiveLoginRecord.js"
import estimateTableHeight from '@/mixins/estimateTableHeight'//vue项目中element-tabel自动计算高度
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeNoDateScope } from "@/api/system/user";

export default {
    archive_Dicts: ['JZ'],
    props: ['isDeitOpen', 'rowIds'],
    components: {
        Treeselect
    },
    watch: {
        rowIds: {
            handler: function (newVal, oldVal) {
                if (newVal != undefined) {
                    this.title="编辑"
                    archiveEnrollDetile(newVal.toString()).then(res => {
                        res.data.receptionTime= res.data.receptionTime+"";
                        this.form =res.data
                    })
                }else{
                    this.title="添加"
                }
            },
            immediate: true
        }
    },
    data() {
        return {
            title: "1",
            deptOptions: [],
            optionSelect: [],
            rules: {
                title: [
                    { required: true, message: '请输入档案名称', trigger: 'blur' },
                ],
                categoryId: [
                    { required: true, message: '请选择档案门类', trigger: 'change' }
                ],
                entriesNum: [
                    { required: true, message: '请输入条目数量', trigger: 'blur' }
                ],
                operationsNum: [
                    { required: true, message: '请输入操作数量', trigger: 'blur' }
                ],
                transferUnit: [
                    { required: true, message: '请选择移交单位', trigger: 'change' }
                ]
            },
            form: {
                title: undefined,
                userName: undefined,
                categoryId: undefined,
                medium: undefined,
                entriesNum: undefined,
                operationsNum: undefined,
                receptionTime: undefined,
                transferUnit: undefined,
                notes: undefined,
            }
        }
    },
    created() {
        if (this.$store.getters.size == 'small') {

            this.isStyle = true

        } else {

            this.isStyle = false
        }
        // this.getList();
        this.getDeptSelect();
        this.deptTreeSelectPost();
    },
    methods: {
        // 只能选择数据下面有children 的数据，没有children的时候禁用
        normalizer(node) {
            if (node.children == null || node.children == undefined || !node.children.length) {
                delete node.children;
            }
            return node
        },


        // 只能选择数据下面有children 的数据，没有children的时候禁用
        normalizerCategoryID(node) {
            if (node.nodeType == 'fonds') {
                if (node.children == undefined || node.children == null || !node.children.length) {

                    node['isDisabled'] = true;
                }
            }
            if (node.children == null || node.children == undefined || !node.children.length) {
                delete node.children;
            }
            return node
        },
        /** 档案门类列表*/
        getDeptSelect(valNew) {
            listGROUPTree().then(response => {
                this.deptOptions = response.data
            });
        },
        //部门（单选）
        deptTreeSelectPost() {
            deptTreeNoDateScope({ tenantCode: this.tenantCode }).then(response => {
                this.optionSelect = response.data;
            })
        },


        //取消
        closeDelDialog() {
            var that = this;
            that.$emit("closeDelDialog")
        },
        //保存
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if(this.rowIds!=undefined){
                        archiveEnrollDeit(this.form).then(res => {
                        if (res.code = "200") {
                            this.$modal.msgSuccess("修改成功");
                            this.$emit("submitForm")
                        }
                    })
                    }else{
                        archiveEnroll(this.form).then(res => {
                        if (res.code = "200") {
                            this.$modal.msgSuccess("新增成功");
                            this.$emit("submitForm")
                        }
                    })
                    }
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 表单重置
        reset() {
            this.form = {
                registeredUnit: null,
                registeredPerson: null,
                userName: null,
                loginName: null,
                passWord: null,
                remark: null,
                tenantCode: null,
                fondsCode: null,
                state: "1"   //1=关闭，0=开启
            };
            this.resetForm("form");
        },
    }
}
</script>
<style lang="scss" scoped></style>